/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from "antd";
import {
  getList,
  onOff,
  numberLimit
} from "@/services/orderSetting";
import { isArray } from "@/utils/utils";

export default {
  namespace: "orderNumSet",

  state: {
    orderNumSetList: {
      lsit: [],
      paganation: null
    }
  },

  effects: {
    *getList({ payload, __dva_resolve, __dva_reject }, { put, call }) {
      const response = yield call(getList, payload);
      if (!response) return;
      const { code, data : { list, count, page } } = response;
      if (code == 0 && isArray(list)) {
        yield put({
          type: "save",
          payload: {
            orderNumSetList:{
              list,
              pagination: {
                total: count,
                current: page,
                pageSize: 20,
                showQuickJumper: false,
                showSizeChanger: false,
              }
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject(response)
      }
    },
    *onOff({ payload, __dva_resolve, __dva_reject }, { put, call }) {
      const response = yield call(onOff, payload);
      if (!response) return;
      const { msg, code, data } = response;
      if (code == 0) {
        message.success(msg)
        __dva_resolve(response);
      } else {
        message.error(msg)
        __dva_reject(response)
      }
    },
    //单号限额
    *setNumber({ payload, __dva_resolve, __dva_reject }, { put, call }) {
      const response = yield call(numberLimit, payload);
      if (!response) return;
      const { msg, code, data } = response;
      if (code == 0) {
        message.success(msg)
        __dva_resolve(response);
      } else {
        message.error(msg)
        __dva_reject(response)
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};